var gulp = require('gulp'),
    sass = require('gulp-sass'),
    minifycss = require('gulp-minify-css'),
    uglify = require('gulp-uglify'),
    jshint = require('gulp-jshint'),
    plumber = require('gulp-plumber');

gulp.task('dist-app-html', function () {
    gulp.src('app/html/**/*')
        .pipe(plumber())
        .pipe(gulp.dest('D:/${packageCode!}/www/default/ie6+/'));

    gulp.src('app/template/**/*')
        .pipe(plumber())
        .pipe(gulp.dest('D:/${packageCode!}/www/default/ie6+/template/'));
});

gulp.task('dist-app-scss', function () {
    gulp.src(['app/scss/**/*.scss'])
        .pipe(plumber())
        .pipe(sass())
        .pipe(minifycss())
        .pipe(gulp.dest('D:/${packageCode!}/www/default/ie6+/'));
});

gulp.task('dist-app-js', function () {
    gulp.src('app/controller/**/*')
        .pipe(plumber())
        .pipe(jshint('.jshintrc'))
        .pipe(jshint.reporter('default'))
        .pipe(uglify())
        .pipe(gulp.dest('D:/${packageCode!}/www/default/ie6+/controller/'));
		
    gulp.src('app/model/**/*')
        .pipe(plumber())
        .pipe(jshint('.jshintrc'))
        .pipe(jshint.reporter('default'))
        .pipe(uglify())
        .pipe(gulp.dest('D:/${packageCode!}/www/default/ie6+/model/'));
		
    gulp.src('app/router/**/*')
        .pipe(plumber())
        .pipe(jshint('.jshintrc'))
        .pipe(jshint.reporter('default'))
        .pipe(uglify())
        .pipe(gulp.dest('D:/${packageCode!}/www/default/ie6+/router/'));

    gulp.src('app/view/**/*')
        .pipe(plumber())
        .pipe(jshint('.jshintrc'))
        .pipe(jshint.reporter('default'))
        .pipe(uglify())
        .pipe(gulp.dest('D:/${packageCode!}/www/default/ie6+/view/'));

    gulp.src('app/action/**/*')
        .pipe(plumber())
        .pipe(jshint('.jshintrc'))
        .pipe(jshint.reporter('default'))
        .pipe(uglify())
        .pipe(gulp.dest('D:/${packageCode!}/www/default/ie6+/action/'));

    gulp.src('app/lib/**/*.js')
        .pipe(plumber())
        .pipe(jshint('.jshintrc'))
        .pipe(jshint.reporter('default'))
        .pipe(uglify())
        .pipe(gulp.dest('D:/${packageCode!}/www/default/ie6+/lib/'));
});

gulp.task('dist-api-json', function () {
    gulp.src('api/**/*.json')
        .pipe(jshint.reporter('default'))
        .pipe(gulp.dest('D:/${packageCode!}/www/default/api/'));
});

gulp.task('dist', function () {
    gulp.src('bower_components/**')
        .pipe(plumber())
        .pipe(gulp.dest('D:/${packageCode!}/www/default/ie6+/lib/'));

    gulp.src('lib/**')
        .pipe(plumber())
        .pipe(gulp.dest('D:/${packageCode!}/www/default/ie6+/lib/'));

    gulp.src('api/**')
        .pipe(plumber())
        .pipe(gulp.dest('D:/${packageCode!}/www/default/'));

    gulp.src('image/**')
        .pipe(plumber())
        .pipe(gulp.dest('D:/${packageCode!}/www/default/image/'));
});

gulp.task('default', ['dist-app-html', 'dist-app-scss', 'dist-app-js', 'dist'], function () {
});

gulp.task('watch', ['default'], function () {
    gulp.watch('app/**/*.html', ['dist-app-html']);
    gulp.watch('app/scss/**/*.scss', ['dist-app-scss']);
    gulp.watch('app/**/*.js', ['dist-app-js']);
    gulp.watch('api/**/*.json', ['dist-api-json']);
});